package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.UserAddress;
import com.shijie.domain.entity.UserScores;
import com.shijie.domain.entity.Users;
import com.shijie.service.LogsService;
import com.shijie.service.UserAddressService;
import com.shijie.service.UserScoresService;
import com.shijie.service.UsersService;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/user")
public class UsersController {

    @Resource
	private UsersService usersService;
    @Resource
    private UserAddressService userAddressService;
    @Resource
    private UserScoresService  userScoresService;
    @Resource
    private LogsService logService;

    @RequiresPermissions(value={"users:list","vip:list"})
	@GetMapping
	public Result list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize,Users user) {
		return ResultGenerator.success(usersService.selectUsersByAll(page, pageSize, user));
	}
	
    @RequiresPermissions(value={"users:address:list"})
	@GetMapping("address")
	public Result address(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) Integer userId) {
		PageHelper.startPage(page, pageSize);
		Condition condition = new Condition(UserAddress.class);
		if(userId != null){
			condition.createCriteria().andEqualTo("userId", userId);
		}
		List<UserAddress> list = userAddressService.selectByCondition(condition);
		
		PageInfo<UserAddress> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}
	
    @RequiresPermissions(value={"users:address:defalut"})
	@PutMapping("address/{addressId}")
	public Result address(@PathVariable Integer addressId) {
    	userAddressService.defalutUserAddress(addressId);
		return ResultGenerator.success();
	}
    
    @RequiresPermissions(value={"users:scores:list"})
   	@GetMapping("scores")
   	public Result address(@RequestParam(defaultValue = "1") Integer page,
   			@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String  q) {
   		PageHelper.startPage(page, pageSize);
   		Condition condition = new Condition(UserScores.class);
   		if(!StringUtils.isEmpty(q)){
   			condition.createCriteria().andLike("userName", "%"+q+"%");
   		}
   		List<UserScores> list = userScoresService.selectByCondition(condition);
   		
   		PageInfo<UserScores> pageInfo = new PageInfo<>(list);
   		return ResultGenerator.success(pageInfo);
   	}
    
    @RequiresPermissions(value={"users:list:edit","vip:edit"})
    @Log(title = "用户管理", action = BusinessType.UPDATE)
    @PutMapping
    public Result update(@RequestBody Users users) {
        usersService.updateByPrimaryKeySelective(users);
        return ResultGenerator.success();
	}
    
    @RequiresPermissions(value={"users:list:details","vip:detail"})
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        return ResultGenerator.success(usersService.getUserDetail(id));
    }
    
    @GetMapping("/statistics")
    public Result statistics(@RequestParam(defaultValue="0") Integer  queryType,@RequestParam(defaultValue="30") Integer  count) {
        return ResultGenerator.success(logService.statistics(queryType,count));
    }
}

